<?php
/**
 * 客户端接口请求类
 *
 * 请妥善保存app_key和secret_key, 以免数据泄露
 *
 * @param string $app_key
 * @param string $secret_key
 */
namespace AuctionCore\Common;

use AuctionCore\Common\Xcrypt;

class Request
{
    private $appKey = '';
    private $secretKey = '';
    private $apiUrl = '';
    private $accessToken = '';

    public function __construct()
    {
        $antNestConf = C("ANT_NEST");
        $this->apiUrl = $antNestConf['ANT_URL']."/";
        $this->appKey = $antNestConf['APP_KEY'];
        $this->secretKey = $antNestConf['SECRET_KEY'];

        $xCrypt = new Xcrypt($antNestConf['SECRET_KEY'], 'ecb', 'auto');
        $timeNow = time();
        $access_key = $antNestConf['APP_KEY'] . '_' . $antNestConf['SECRET_KEY'] . '_' . $timeNow;
        $this->accessToken = $xCrypt->encrypt($access_key, 'base64');
    }

    /**
     * 请求接口返回内容
     * @param string $urlPath [请求的URL地址]
     * @param string $params [header权限校验参数]
     * @param int $isPost [请求的参数]
     * @return bool|mixed [是否采用POST形式]
     *
     */
    public function juHeCurl($urlPath, $params = '', $isPost = 0)
    {
        $url = $this->apiUrl . $urlPath;
        $httpInfo = array();
        $ch = curl_init();
        $header = array(
            'appkey:' . $this->appKey,
            'token:' . $this->accessToken,
        );
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; 
        SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        // curl_setopt ($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1');
        // curl_setopt($ch,CURLOPT_COOKIESESSION,true);
        curl_setopt($ch, CURLOPT_COOKIE, 'XDEBUG_SESSION=1');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        $params = json_decode($params, true);
        $data = http_build_query($params);
        /*备注： 如果后期需要对http通讯过程中的数据做加密传输，则打开以下代码*/
        // $m = new \Common\Org\Xcrypt($key, 'ecb', 'auto');
        // $data = $m->encrypt($a);
        // $data .= '&XDEBUG_SESSION_START=sublime.xdebug';
        if ($isPost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($data) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $data);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $response = curl_exec($ch);
        if ($response === false) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        //\Think\Log::record($response, 'DEBUG', true);
        return json_decode($response, true);
    }
}
